/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import hilog from '@ohos.hilog';
import {Http2NapiUtils} from '../../ets/http2/index';

export class WrapperResolver {
  private DOMAIN = 0x8691
  private TAG = 'WrapperResolver'

  private serverList: string[];

  constructor() {
    hilog.info(this.DOMAIN, this.TAG, 'create Resolver');
  }

  public getServers(): string[] {
    return [
      '8.8.8.8',
      '2001:4860:4860::8888',
      '8.8.8.8:1053',
      '[2001:4860:4860::8888]:1053',
    ]
  }

  public setServers(paramList: string[]) {
    this.serverList = paramList;
  }

  public resolve() {

  }

  public resolve4(param: string) {
    return param;
  }

  public resolve6(param: string) {
    return param;
  }

  public resolveAny() {
  }

  public resolveCaa() {
  }

  public resolveCname() {
  }

  public resolveMx() {
  }

  public resolveNaptr() {
  }

  public resolveNs() {
  }

  public resolvePtr() {
  }

  public resolveSoa() {
  }

  public resolveSrv() {
  }

  public resolveTxt(param: string): Promise<string[][]> {
    return dns.resolveTxt(param);
  }

  public reverse() {
  }
}

export class dns {
  static async lookup(hostname: string): Promise<string> {
    return await Http2NapiUtils.dnsLookup(hostname)
  }

  static Resolver() {
    return new WrapperResolver();
  }

  static convertStringToArray(input: string): string[][] {
    let rows = input.split(',');
    let result: string[][] = [];
    for (let row of rows) {
      let elements = row.split(';');
      result.push(elements);
    }
    return result;
  }

  static async resolveTxt(hostname: string): Promise<string[][]> {
    let txt = await Http2NapiUtils.dnsResolveTxt(hostname);
    return dns.convertStringToArray(txt);
  }
}

export interface LookupAddress {
  address: string;
  family: number;
}

